//
//  CJWelcomeViewController.swift
//  SinaWeibo
//
//  Created by 程建 on 16/9/26.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

private let bottomMargin: CGFloat = 120
class CJWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //设置背景颜色
        view.backgroundColor = UIColor.white
        
        setupUI()
    }
    
    //执行动画需要放在视图已经显示的生命周期的方法
    override func viewDidAppear(_ animated: Bool) {
        
        //执行动画
        startAnimation()
    }
    
    
    //执行动画的方法
    func startAnimation() {
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
            let offsetY = -(UIScreen.main.bounds.height - bottomMargin - 85)
            
            self.iconView.snp.makeConstraints({ (make) in
                make.bottom.equalTo(self.view).offset(offsetY)
            })
            //更新约束 自动布局的控件有frame吗?  有!!!!!!!, 为什么,没有frame怎么显示!!!!
            //自动布局是一种根据约束来自动给控件设置frame
            //自动布局的控件不能够直接修改frame 而应该修改约束
            //更新约束 使用update
            //更新的约束是立即修改frame? 不是的!!!!!!!!
            //frame是在layoutSubviews方法中根据已经设置好的约束来自动修改控件的frame
            //提前布局子视图   -> layoutSubviews -> 动画时间才会奏效
            
            self.view.layoutIfNeeded()
            
            }) { (_) in
                UIView.animate(withDuration: 1, animations: { 
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) in
                        print("ok")
                        //欢迎 界面切换到UITabBarController 界面
                        //不建议使用modal的方式切换视图控制器,而是使用切换根视图控制器
                       //发布通知
                        NotificationCenter.default.post(name: NSNotification.Name(KchangeRootViewController), object: nil)
                        
                })
        }
        
        
        
    }
    

    func setupUI() {
        
        view.addSubview(iconView)
        
        view.addSubview(welcomeLabel)
    
        iconView.snp.makeConstraints { (make) in
            make.bottom.equalTo(view).offset(-bottomMargin)
            make.centerX.equalTo(view)
            //设置图片的宽度
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.bottom).offset(20)
            make.centerX.equalTo(view)
        }
        
        //设置图片的url地址
       iconView .sd_setImage(with: CJUserAccountViewModel.shardAccountViewModel.iconURL) 
        
    }
    
    
    
    //懒加载
   private lazy var iconView: UIImageView = {
        
        let iv = UIImageView(image: #imageLiteral(resourceName: "avatar_default_big"))
        //设置圆角
        iv.cornerRadius = 42
        //设置边线
        iv.layer.borderWidth = 1
        //设置外边线颜色
        iv.layer.borderColor = UIColor.orange.cgColor
    
        return iv
    }()
   
    private lazy var welcomeLabel: UILabel = {
        
        let l = UILabel(title: "欢迎归来", textColor: UIColor.darkGray, fontSize: 16)
        
        l.alpha = 0
        
        return l
    }()
    
    
}
